/*
 * Sylvain BERTRAND <digital.ragnarok@gmail.com>
 * code protected by GNU GPL v3
 */
#include <X11/Xlib.h>
#include <glib.h>
#include <libxml/xinclude.h>
#include "../../toolkit/keyboard.h"
#include "../../toolkit/xml.h"
#include "../misc.h"
#include "../geom.h"
#include "../frame.h"
#include "../actions.h"
#include "../window.h"
#include "../stacking.h"
#include "../client.h"
#include "../focus.h"
struct options {
	gboolean here;
	gboolean stop_int;
};

static gpointer setup_func(xmlNodePtr node)
{
	xmlNodePtr n;
	struct options *o;
	o = g_slice_new0(struct options);
	o->stop_int = TRUE;
	if((n = t_xml_find_node(node, "here")))
		o->here = t_xml_node_bool(n);
	if((n = t_xml_find_node(node, "stopInteractive")))
		o->stop_int = t_xml_node_bool(n);
	return o;
}

static void free_func(gpointer o)
{
	g_slice_free(struct options, o);
}

static gboolean run_func(struct wm_actions_data *data, gpointer options)
{
	struct options *o = options;
	if(data->client) {
		if(data->button == 0 || client_mouse_focusable(data->client) || (data->context != WM_FRAME_CONTEXT_CLIENT
				&& data->context != WM_FRAME_CONTEXT_FRAME)) {
			if(o->stop_int)
				actions_interactive_cancel_act();
			actions_client_move(data, TRUE);
			client_activate(data->client, TRUE, o->here, FALSE, TRUE);
			actions_client_move(data, FALSE);
		}
	} else if(data->context == WM_FRAME_CONTEXT_DESKTOP) {
		if(o->stop_int)
			actions_interactive_cancel_act();
		focus_nothing();
	}
	return FALSE;
}

void action_focus_startup(void)
{
	actions_register("Focus", setup_func, free_func, run_func);
}
